package socket编程计算器;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class Client extends Application {
    // Io streams
    DataOutputStream toServer = null;
    DataInputStream fromServer = null;

    @Override  // Override the start method in the Application class  覆盖 Application 类中的 start 方法
    public void start(Stage primaryStage) {
        // Panel p to hold the label and text field  面板 p 用于保存标签和文本字段
        BorderPane paneForTextField = new BorderPane();
        paneForTextField.setPadding(new Insets(5, 5, 5, 5));
        paneForTextField.setStyle("-fx-border-color: green");
        paneForTextField.setLeft(new Label("Enter a radius: "));

        TextField tf = new TextField();
        tf.setAlignment(Pos.BOTTOM_RIGHT);
        paneForTextField.setCenter(tf);

        BorderPane mainPane = new BorderPane();
        // Text area to display contents
        // 显示内容的文本区域
        TextArea ta = new TextArea();
        mainPane.setCenter(new ScrollPane(ta));
        mainPane.setTop(paneForTextField);

        // Create a scene and place it in the stage
        // 创建一个场景并将其放置在舞台上
        Scene scene = new Scene(mainPane, 450, 200);
        primaryStage.setTitle("Client");  // Set the stage title  设置舞台标题
        primaryStage.setScene(scene);  // Place the scene in the stage  将场景放置在舞台上
        primaryStage.show();  // Display the stage 展示舞台

        tf.setOnAction(e -> {
            try {
                // Get the radius from the text field  从文本字段中获取半径
                double radius = Double.parseDouble(tf.getText().trim());
                // Send the radius to the server  将半径发送到服务器
                toServer.writeDouble(radius);
                toServer.flush();
                // Get area from the server   从服务器获取区域
                double area = fromServer.readDouble();
                // Display to the text area  显示到文本区域
                ta.appendText("Radius is " + radius + "\n");
                ta.appendText("Area received from the server is "
                        + area + '\n');
            } catch (IOException ex) {
                System.err.println(ex);
            }
        });

        try {
            // Create a socket to connect to the server  创建一个套接字以连接到服务器
            Socket socket = new Socket("localhost", 8000);
//            Socket socket = new Socket("也可以放IP地址", 8000);

            // Create an input stream to receive data from the server  创建输入流以从服务器接收数据
            fromServer = new DataInputStream(socket.getInputStream());
            // Create an output stream to receive data from the server  创建输出流以从服务器接收数据
            toServer = new DataOutputStream(socket.getOutputStream());
        } catch (IOException ex) {
            ta.appendText(ex.toString() + '\n');
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
